package com.example.studentknowledgesys.dao;

import com.example.studentknowledgesys.dto.ArticleDetailDTO;
import com.example.studentknowledgesys.entity.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 光的代言人
 */
@Mapper
public interface ArticleMapper{
    // 插入文章
    @Insert("INSERT INTO articles(title, content, tag_id, author_id) " +
            "VALUES(#{title}, #{content}, #{tagId}, #{authorId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertArticle(Article article);

    // 更新文章
    @Update("UPDATE articles SET title = #{title}, content = #{content}, " +
            "tag_id = #{tagId}, status=#{status},updated_at = NOW() WHERE id = #{id}")
    int updateArticle(Article article);

    // 删除文章
    @Delete("DELETE FROM articles WHERE id = #{id}")
    int deleteArticleById(Long id);

    // 根据ID查询文章
    @Select("SELECT * FROM articles WHERE id = #{id}")
    Article selectArticleById(Long id);

    // 分页查询文章
    @Select("SELECT * FROM articles ORDER BY updated_at DESC")
    List<Article> selectAllArticles();

    // 根据标签查询文章
    @Select("SELECT * FROM articles WHERE tag_id = #{tagId}")
    List<Article> selectArticlesByTagId(Long tagId);

    List<ArticleDetailDTO> getArticleDetails(@Param("tagName") String tagName,  @Param("authorId")Long authorId);

    ArticleDetailDTO getSingleArticleDetail(Long articleId);
    // 编辑文章
    int updateArticleById(Article article);
    // 更改文章状态
    int updateArticleStatus(@Param("id")Long articleId, @Param("status")Integer status);
}
